package com.wuming.caiwu.service.impl;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.WumingErrorCode;
import com.wuming.caiwu.dao.SysUserMapper;
import com.wuming.caiwu.model.SysUser;
import com.wuming.caiwu.model.SysUserDetail;
import com.wuming.caiwu.service.SysUserService;
import com.wuming.caiwu.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("sysUserService")
public class SysUserServiceImpl extends BaseServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysRoleMapper;

    @Override
    public void saveSysUser(SysUser member) throws BusinessException {
        member.setCreateTime(new Date());
        sysRoleMapper.insert(member);
    }

    @Override
    public void modifySysUser(SysUser member) throws BusinessException {
        sysRoleMapper.updateByPrimaryKey(member);
    }

    @Override
    public SysUser getSysUserById(Long id) throws BusinessException {
        return sysRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public void removeSysUserById(Long id) throws BusinessException {
        sysRoleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<SysUser> getSysUsersByPage(String username, Long qsgStoreId, int pageIndex, int pageSize) throws BusinessException {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(username)) {
            params.put("username", username);
        }
        if(null != qsgStoreId){
            params.put("qsgStoreId", qsgStoreId);
        }
        try {
            return (List<SysUser>) this.getPageList(SysUserMapper.class, "selectByParams", params,pageIndex,pageSize);
        } catch (Exception e) {
            logger.error("查询用户信息总数失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotal(String username, Long qsgStoreId) throws BusinessException {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(username)) {
            params.put("username", username);
        }
        if(null != qsgStoreId){
            params.put("qsgStoreId", qsgStoreId);
        }
        try {
            return this.getTotal(SysUserMapper.class, "selectTotal", params);
        } catch (Exception e) {
            logger.error("查询用户信息总数失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public SysUserDetail getUserDetailById(Long userId) throws BusinessException {
        return sysRoleMapper.selectUserRole(userId);
    }

}
